package com.three.iot.rest.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.three.iot.api.SysResourceService;
import com.three.iot.api.SysRoleResourceService;
import com.three.iot.commons.dto.SysRoleResourceCondDto;
import com.three.iot.commons.dto.SysRoleResourceDto;
import com.three.iot.commons.dto.SysRoleResourceResultDto;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.commons.utils.NumberValidationUtils;
import com.three.iot.rest.utils.RequestUtils;
import com.three.iot.rest.utils.ValidatorUtil;
import com.three.iot.rest.vo.admin.SysRoleResourceCondVo;
import com.three.iot.rest.vo.admin.SysRoleResourceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Author:luiz
 * @Date: 2018/3/12 15:10
 * @Descripton:
 * @Modify :
 **/
@RestController
@RequestMapping("/admin/sysRoleResource")
@Api(tags = "角色资源", description = "角色资源相关api")
public class SysRoleResourceController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    SysRoleResourceService sysRoleResourceService;
    @Autowired
    SysResourceService sysResourceService;

    /**
     * restful api 增删改查
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ApiOperation(value = "创建角色资源", notes = "根据角色资源对象创建角色资源")
    @ApiParam(name = "sysRoleResourceVo", value = "角色资源信息实体 sysRoleResourceVo", required = true)
    public Result<String> create(@RequestBody SysRoleResourceVo sysRoleResourceVo, HttpServletRequest request) {
        try {
            ValidatorUtil.validateEntity(sysRoleResourceVo);//校验
            SysRoleResourceDto sysRoleResourceDto = new SysRoleResourceDto();
            BeanUtils.copyProperties(sysRoleResourceVo, sysRoleResourceDto);
            sysRoleResourceDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            return sysRoleResourceService.create(sysRoleResourceDto);
        } catch (Exception e) {
            logger.error("添加角色资源异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ApiOperation(value = "保存角色资源", notes = "批量保存角色资源")
    @ApiParam(name = "sysRoleResourceVos", value = "角色资源信息实体 sysRoleResourceVos", required = true)
    public Result<String> save(@RequestBody List<SysRoleResourceVo> sysRoleResourceVos, HttpServletRequest request) {
        try {
            List<SysRoleResourceDto> sysRoleResourceDtos = Lists.newArrayList();
            for (SysRoleResourceVo sysRoleResourceVo : sysRoleResourceVos) {
                SysRoleResourceDto sysRoleResourceDto = new SysRoleResourceDto();
                BeanUtils.copyProperties(sysRoleResourceVo, sysRoleResourceDto);
                sysRoleResourceDto.setUserNo(RequestUtils.getCurrentUserNo(request));
                sysRoleResourceDtos.add(sysRoleResourceDto);
            }
            return sysRoleResourceService.save(sysRoleResourceDtos);
        } catch (Exception e) {
            logger.error("保存角色资源异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "删除角色资源", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "角色资源编号", required = true, dataType = "string", paramType = "path")
    public Result<String> delete(@PathVariable("id") String id) {
        try {
            if (!NumberValidationUtils.isRealNumber(id)) {
                return Result.newError(ResultCode.COMMON_PARAM_INVALID);
            }
            return sysRoleResourceService.delete(id);
        } catch (Exception e) {
            logger.error("删除角色资源异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @RequestMapping(value = "/{ids}", method = RequestMethod.POST)
    @ApiOperation(value = "批量删除角色资源", notes = "根据url的ids来指定删除对象")
    @ApiParam(name = "ids", value = "角色资源编号", required = true)
    public Result<String> deleteByIds(@RequestBody List<String> ids) {
        try {
            return sysRoleResourceService.deleteByIds(ids);
        } catch (Exception e) {
            logger.error("批量删除角色资源异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @RequestMapping(value = "/{roleCode}", method = RequestMethod.GET)
    @ApiOperation(value = "获取角色资源详细信息", notes = "根据url的roleCode来获取角色资源详细信息")
    @ApiImplicitParam(name = "roleCode", value = "角色资源编号", required = true, dataType = "string", paramType = "path")
    public Result<List<SysRoleResourceResultDto>> findByRoleCode(@PathVariable("roleCode") String roleCode) {
        return sysRoleResourceService.findByRoleCode(roleCode);
    }


    @RequestMapping(value = "listPage", method = RequestMethod.POST)
    @ApiOperation(value = "角色资源分页查询", notes = "角色资源分页查询")
    @ApiParam(name = "sysRoleResourceCondVo", value = "角色资源分页查询 sysRoleResourceCondVo", required = true)
    public Result listPage(@RequestBody SysRoleResourceCondVo sysRoleResourceCondVo) {
        try {
            ValidatorUtil.validateEntity(sysRoleResourceCondVo);//校验
            SysRoleResourceCondDto sysRoleResourceCondDto = new SysRoleResourceCondDto();
            BeanUtils.copyProperties(sysRoleResourceCondVo, sysRoleResourceCondDto);
            Page<SysRoleResourceResultDto> sysRoleResultDtoPage = sysRoleResourceService.listPage(sysRoleResourceCondDto);
            PageInfo pageInfo = new PageInfo(sysRoleResultDtoPage);
            return Result.newSuccess(pageInfo);
        } catch (Exception e) {
            logger.error("分页查询角色资源异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }
}
